﻿using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json.Linq;
using ProjScan.Tools;

namespace ProjScan.Config
{
	// Token: 0x02000327 RID: 807
	internal class ConfigMgr : Singleton<ConfigMgr>
	{
		// Token: 0x060012D4 RID: 4820 RVA: 0x0005A074 File Offset: 0x00058274
		public ConfigMgr()
		{
			this.dicConfigs.Add(typeof(GlobalConfig), new GlobalConfig());
			this.dicConfigs.Add(typeof(BasicAssetsCheckConfig), new BasicAssetsCheckConfig());
			this.dicConfigs.Add(typeof(ScenesCheckConfig), new ScenesCheckConfig());
			this.dicConfigs.Add(typeof(GlobalSettingsConfig), new GlobalSettingsConfig());
			this.dicConfigs.Add(typeof(CSCheckConfig), new CSCheckConfig());
			this.dicConfigs.Add(typeof(LuaCheckConfig), new LuaCheckConfig());
			this.dicConfigs.Add(typeof(CustomRulesCheckConfig), new CustomRulesCheckConfig());
			this.dicConfigs.Add(typeof(EffectsPlayCheckConfig), new EffectsPlayCheckConfig());
			this.dicConfigs.Add(typeof(ShaderAnalysisConfig), new ShaderAnalysisConfig());
			this.dicConfigs.Add(typeof(ArtAssetsCheckConfig), new ArtAssetsCheckConfig());
			this.dicConfigs.Add(typeof(ScanOverviewConfig), new ScanOverviewConfig());
			foreach (KeyValuePair<Type, BaseConfig> keyValuePair in this.dicConfigs)
			{
				keyValuePair.Value.Init();
			}
		}

		// Token: 0x060012D5 RID: 4821 RVA: 0x0005A210 File Offset: 0x00058410
		public void InitConfig()
		{
			this.isInit = false;
			foreach (KeyValuePair<Type, BaseConfig> keyValuePair in this.dicConfigs)
			{
				keyValuePair.Value.Init();
			}
			this.isInit = true;
		}

		// Token: 0x060012D6 RID: 4822 RVA: 0x0005A280 File Offset: 0x00058480
		public void LoadConfig()
		{
			bool flag = !this.isInit;
			if (flag)
			{
				throw new Exception("ConfigMgr 还没有初始化");
			}
			this.ReadConfig();
			float num = 0f;
			foreach (KeyValuePair<Type, BaseConfig> keyValuePair in this.dicConfigs)
			{
				num += 1f;
				ToolsHelper.ShowProgressBar("Manager", "Loading config info...", num / (float)this.dicConfigs.Count, false);
				keyValuePair.Value.LoadConfig(this.congfig);
			}
		}

		// Token: 0x060012D7 RID: 4823 RVA: 0x0005A334 File Offset: 0x00058534
		private void ReadConfig()
		{
			string text = Path.Combine(ToolsHelper.GetProjectLocationPath(), "ProjScanConfig.json");
			bool flag = !Singleton<ScanInfo>.Instance.isScanQuit;
			if (flag)
			{
				bool flag2 = File.Exists(text);
				if (flag2)
				{
					try
					{
						string text2 = File.ReadAllText(text);
						this.congfig = JObject.Parse(text2);
						ScanLog.UnityLog("使用项目配置文件 " + text);
						return;
					}
					catch (Exception arg)
					{
						ScanLog.UnityLog(string.Format("解析项目配置文件失败！ {0}，尝试使用框架默认配置。\n{1}", text, arg));
					}
				}
			}
			else
			{
				ScanLog.UnityLog("扫描模式下，跳过读取项目本地的配置文件逻辑");
			}
			string text3 = Path.Combine(ToolsHelper.GetLocationPath(), "ProjScanConfig.json");
			ScanLog.UnityLog("使用框架默认配置文件 " + text);
			bool flag3 = !File.Exists(text3);
			if (flag3)
			{
				ScanLog.UnityLogError("配置文件未找到:" + text3);
				ToolsHelper.DisplayDialog("温馨提示", "未找到配置文件：ProjScanConfig.json，请从压缩包中拷贝到工程目录下。", "好的");
			}
			string text4 = File.ReadAllText(text3);
			this.congfig = JObject.Parse(text4);
		}

		// Token: 0x060012D8 RID: 4824 RVA: 0x0005A444 File Offset: 0x00058644
		public BaseConfig GetConfig<T>() where T : BaseConfig
		{
			Type typeFromHandle = typeof(T);
			return this.dicConfigs[typeFromHandle];
		}

		// Token: 0x060012D9 RID: 4825 RVA: 0x0005A470 File Offset: 0x00058670
		public BaseConfig GetConfig(Type baseConfigType)
		{
			bool flag = !baseConfigType.IsSubclassOf(typeof(BaseConfig));
			if (flag)
			{
				throw new ArgumentException("Invalid Type. Must be a subclass of BaseConfig");
			}
			BaseConfig result;
			bool flag2 = this.dicConfigs.TryGetValue(baseConfigType, out result);
			if (flag2)
			{
				return result;
			}
			throw new Exception("No config found for the provided Type.");
		}

		// Token: 0x060012DA RID: 4826 RVA: 0x0005A4C4 File Offset: 0x000586C4
		public void SaveAllConfig()
		{
			JObject jobject = new JObject();
			foreach (KeyValuePair<Type, BaseConfig> keyValuePair in this.dicConfigs)
			{
				keyValuePair.Value.SaveConfig(jobject);
			}
			string path = Path.Combine(ToolsHelper.GetProjectLocationPath(), "ProjScanConfig.json");
			File.WriteAllText(path, jobject.ToString());
		}

		// Token: 0x04000B3B RID: 2875
		private Dictionary<Type, BaseConfig> dicConfigs = new Dictionary<Type, BaseConfig>();

		// Token: 0x04000B3C RID: 2876
		private JObject congfig;

		// Token: 0x04000B3D RID: 2877
		private bool isInit;
	}
}
